<?php

namespace App\Http\Middleware;

use App\Models\Business\Business as BusinessModel;
use Closure;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;

class Auth
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        // 接收用户id
        $busid = $request->input('busid',0);

        // 查询用户是否存在
        $business = BusinessModel::find($busid);

        // 如果不存在就不往下走
        if(!$business)
        {
            return error('非法登录');
        }

        // 传参
        $request->attributes->add(['business' => $business]);

        return $next($request);
    }
}
